package com.example.imooc_utils.stream;


import com.alibaba.fastjson.JSON;
import com.example.imooc_utils.lambda.pojo.Sku;
import com.example.imooc_utils.lambda.service.SkuService;
import com.example.imooc_utils.utils.SkuCategoryEnum;
import org.junit.Before;
import org.junit.Test;

import java.util.*;

/**
 * 演示流的各种操作
 */
public class StreamOperator {

    List<Sku> list;

    @Before
    public void init(){
        list =SkuService.getCartSkuList();
    }


    /**
     *  filter 接收Predicate函数式接口 返回boolean类型
     */
    @Test
    public void filterTest(){
        list.stream()
                .filter( sku -> SkuCategoryEnum.BOOKS.equals(sku.getSkuCategory()))
                .forEach( item -> System.out.println(JSON.toJSONString(item,true)));
    }


    /**
     *  map 接受返回Function 接收一个对象返回另一个对象
     */
    @Test
    public void maptest(){
        list.stream()
                .map( sku -> sku.getSkuName())
                .forEach( item -> System.out.println(JSON.toJSONString(item,true)));
    }

    /**
     *  接收一个Stream流 并将流合并
     */
    @Test
    public void flatMap(){
        list.stream()
                .flatMap(sku -> Arrays.stream( sku.getSkuName().split("")))
                .forEach(item -> System.out.println(JSON.toJSONString(item,true)));
    }

    /**
     * 接收 Consumer 无返回对象
     */
    @Test
    public void peek(){
        list.stream()
                .peek(sku -> System.out.println(JSON.toJSONString(sku,true)) )
                .forEach(item -> System.out.println(JSON.toJSONString(item,true)));
    }


    /**
     * 接收 Consumer 无返回对象
     */
    @Test
    public void sorted(){


//        new ArrayList<Sku>().sort(Comparator.comparingInt( Sku::getSkuId));
        list.stream()
                .peek(sku -> System.out.println("===" +JSON.toJSONString(sku,true)) )
                .sorted(Comparator.comparing(Sku :: getSkuId).reversed())
                .forEach(item -> System.out.println(JSON.toJSONString(item,true)));
    }

    @Test
    public void distinct(){
        list.stream()
                .map(Sku::getSkuCategory)
                .distinct()
                .forEach(item -> System.out.println(JSON.toJSONString(item,true)));
    }

    @Test
    public void skip() {
        list.stream()
                .sorted(Comparator.comparing(Sku::getSkuPrice))
                .skip(3)
                .forEach(item -> System.out.println(JSON.toJSONString(item, true)));
    }

    @Test
    public void limit(){
            list.stream()
                    .sorted(Comparator.comparing(Sku::getSkuPrice))
                    .limit(3)
                    .forEach(item -> System.out.println(JSON.toJSONString(item,true)));
    }


    @Test
    public void allMatch(){
        boolean allMatch = list.stream()
                .allMatch(sku -> sku.getSkuPrice() > 100);
        System.out.println(allMatch);
    }


    @Test
    public void  noneMatch(){
        boolean noneMatch = list.stream()
                .noneMatch(sku -> sku.getSkuPrice() > 100);
        System.out.println(noneMatch);
    }

    @Test
    public void findFirst(){
        Optional<Sku> first = list.stream()
                .findFirst();

        System.out.println(JSON.toJSONString(first.get(),true));
    }

    @Test
    public void findAny(){
        Optional<Sku> any = list.stream()
                .findAny();

        System.out.println(JSON.toJSONString(any.get(),true));
    }
}
